import * as THREE from 'three';
import * as _ from 'lodash';
import { getDefaultState } from "./baseState"
import PointCloud from "@/Main/PointCloud"
import handleHack from "./hack"
import DataResource from './DataResource';
import TaskLoadManager from './TaskLoadManager';
import ViewManager from './ViewManager';
import ConfigManager from './ConfigManager';

export default class BaseEditor extends THREE.EventDispatcher {
  activeView = null;
  idCount = 1;
  pc;
  state;
  currentTrack;
  currentTrackName = '';
  currentClass = '';
  frameMap = new Map();
  frameIndexMap = new Map();
  classMap = new Map();
  needUpdateFilter = true;
  eventSource = '';
  bsState;
  // cmdManager;
  // hotkeyManager;
  // actionManager;
  viewManager;
  configManager;
  // dataManager;
  // businessManager;
  // playManager;
  // loadManager;
  taskLoadManager;
  dataResource;
  // modelManager;
  // trackManager;
  // taskManager;

  // ui
  registerModal = () => { };
  showModal = () => Promise.resolve();
  showMsg = () => { };
  showConfirm = () => Promise.resolve();
  showLoading = () => { };

  constructor() {
    super();
    this.state = getDefaultState();

    this.pc = new PointCloud();
    // this.pc.initTween();

    // this.cmdManager = new CmdManager(this);
    // this.hotkeyManager = new HotkeyManager(this);
    // this.actionManager = new ActionManager(this);

    this.viewManager = new ViewManager(this);
    this.configManager = new ConfigManager(this);

    // this.dataManager = new DataManager(this);
    // this.playManager = new PlayManager(this);
    // this.loadManager = new LoadManager(this);
    // 用于加载任务的初始当前帧段，标注数据及最右侧数据结果
    this.taskLoadManager = new TaskLoadManager(this)
    // 用于解析数据及通过接口将获取的文件数据解析转换成three的相机，及一些数据配置
    this.dataResource = new DataResource(this);
    // this.businessManager = new BusinessManager(this);
    // this.modelManager = new ModelManager(this);
    // this.trackManager = new TrackManager(this);
    // this.taskManager = new TaskManager(this);
    handleHack(this);

    // this.initEvent();
    // util
    // console.log(this, "当前BaseEditor");
    this.blurPage = _.throttle(this.blurPage.bind(this), 40);
  }

  initEvent() {
    let config = this.state.config;
  }

  blurPage() {
    if (document.activeElement && document.activeElement !== document.body) {
      (document.activeElement).blur();
    }
  }

  setFrames(frames) {
    this.state.frames = frames;
    frames.forEach((e, index) => {
        this.frameMap.set(e.id + '', e);
        this.frameIndexMap.set(e.id + '', index);
    });
  }

  taskLoadFrame(index, showLoading = true, force = false) {
    this.taskLoadManager.loadFrame(index, showLoading, force);
  }

  setPointCloudData(data, ground, intensityRange){
    this.pc.setPointCloudData(data);
    this.configManager.updatePointConfig(ground, intensityRange);
    // this.dispatchEvent({ type: Event.POINTS_CHANGE });
  }
}